<?php
  /**
   * This class provides helper static functions to perform IO operations
   *
   * @author Macdonald Terrence Robinson
   * @package Core
   *
   */
  final class FileSystem
  {
    public static function dirExists($dirPath)
    {
      if( !is_dir($dirPath) )
        return false;
        
      if( file_exists($dirPath) )
        return true;
        
      return false;
    }
    
    public static function fileExists($filePath)
    {
      if( !is_file($filePath) )
        return false;
        
      if( file_exists($filePath) )
        return true;
        
      return false;
    }
    
    public static function dirMustExist($dirPath, $errorMessage)
    {
      if( self::dirExists($dirPath) )
        return true;
        
      ErrorHandler::runTimeError($errorMessage);
    }
    
    public static function fileMustExist($filePath, $errorMessage)
    {
      if( self::fileExists($filePath) )
        return true;
        
      ErrorHandler::runTimeError($errorMessage);
    }
    
    public static function scanDir($dir, $removeParents = true)
    {
      $list = scandir($dir);

      $newlist = array();
      foreach ($list as $key=>$value)
      {
      	if ((!$removeParents) || (($removeParents) && (($value != '.') && ($value !='..'))))
          $newlist[] = $list[$key];
      }

      return $newlist;
    }
    
    public static function emptyDir($dirPath, $deleteSubfoldersAndFiles=false )
    {
      if( self::dirExists($dirPath) && !stristr( $dirPath, Config::$writableDir ) )
        return 'Cannot empty directory: ( '.$dirPath.' )';
     
      $allFilesAndFolders = self::scanDir($dirPath);
      
      foreach ($allFilesAndFolders as $key=>$value)
      {
        if( is_file($dirPath.$value) )
          unlink($dirPath.$value);
        else
        {
          if($deleteSubfoldersAndFiles)
            self::recursiveDelete($dirPath.$value);
        }
      }
    }
    
    public static function recursiveDelete($dir)
    {
      if (!file_exists($dir)) return true;
      if (!is_dir($dir)) return unlink($dir);
      foreach (scandir($dir) as $item) {
          if ($item == '.' || $item == '..') continue;
          if (!self::recursiveDelete($dir.DIRECTORY_SEPARATOR.$item)) return false;
      }
      return rmdir($dir);
    }
    
    public static function saveData($filePath, $data, $parseBeforeSaving=false, $permissions = 0755)
    {
      if($parseBeforeSaving)
        $data = Parser::replaceWithTemplateVariables($data);

      file_put_contents($filePath, $data);
      chmod($filePath, $permissions);
    }
    
    public static function deleteFile($filePath)
    {
      if( self::fileExists($filePath) )
        unlink($filePath);
    }
    
    public static function renameFile($oldFileName, $newFileName)
    {
      if( self::fileExists($oldFileName) )
        rename($oldFileName, $newFileName);
    }
    
    public static function readFile($filePath, $parseBeforeReturn=false)
    {
      $content = '';
      
      if( self::fileExists($filePath) )
        $content = file_get_contents($filePath);
        
      if( $parseBeforeReturn )
        $content = Parser::parseData($content);
        
      return $content;
    }
    
    public static function makeDir($newDirPath, $autoCreateParentFolders = false, $permissions = 0755)
    {
      if( !self::dirExists($newDirPath) )
        mkdir($newDirPath, $permissions, $autoCreateParentFolders);
    }
    
    public static function renameDir($oldDirName, $newDirName)
    {
      if( self::dirExists($oldDirName) )
        return rename($oldDirName, $newDirName);

      return false;
    }

    public static function deleteDir($dirPath)
    {
      if( self::dirExists($dirPath) )
        return rmdir($dirPath);
        
      return false;
    }
    
  }
?>
